home *** CD-ROM | disk | FTP | other *** search
/ Game Programming in C++ - Start to Finish / GameProgrammingS.iso / developer_install / CEGUISDK-0.4.1-VC6-Native.exe / {app} / Samples / FalagardDemo1 / src / Sample_FalagardDemo1.cpp next >
Encoding:
C/C++ Source or Header  |  2005-08-28  |  9.1 KB  |  264 lines

  1. /************************************************************************
  2.     filename:   Sample_FalagardDemo1.cpp
  3.     created:    19/6/2005
  4.     author:     Paul D Turner
  5. *************************************************************************/
  6. /*************************************************************************
  7.     Crazy Eddie's GUI System (http://www.cegui.org.uk)
  8.     Copyright (C)2004 - 2005 Paul D Turner (paul@cegui.org.uk)
  9.  
  10.     This library is free software; you can redistribute it and/or
  11.     modify it under the terms of the GNU Lesser General Public
  12.     License as published by the Free Software Foundation; either
  13.     version 2.1 of the License, or (at your option) any later version.
  14.  
  15.     This library is distributed in the hope that it will be useful,
  16.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  17.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  18.     Lesser General Public License for more details.
  19.  
  20.     You should have received a copy of the GNU Lesser General Public
  21.     License along with this library; if not, write to the Free Software
  22.     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  23. *************************************************************************/
  24. #include "Sample_FalagardDemo1.h"
  25. #include "CEGUI.h"
  26. #include "falagard/CEGUIFalWidgetLookManager.h"
  27.  
  28. #if defined( __WIN32__ ) || defined( _WIN32 )
  29. #define WIN32_LEAN_AND_MEAN
  30. #define NOMINMAX
  31. #include "windows.h"
  32.  
  33. int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine,int nCmdShow)
  34. #else
  35. int main(int argc, char *argv[])
  36. #endif
  37. {
  38.     // This is a basic start-up for the sample application which is
  39.     // object orientated in nature, so we just need an instance of
  40.     // the CEGuiSample based object and then tell that sample application
  41.     // to run.  All of the samples will use code similar to this in the
  42.     // main/WinMain function.
  43.     FalagardDemo1Sample app;
  44.     return app.run();
  45. }
  46.  
  47. //////////////////////////////////////////////////////////////////////////
  48. /*************************************************************************
  49.  
  50.     FalagardDemo1Sample class
  51.  
  52. *************************************************************************/
  53. //////////////////////////////////////////////////////////////////////////
  54. /*************************************************************************
  55.     Sample specific initialisation goes here.
  56. *************************************************************************/
  57. bool FalagardDemo1Sample::initialiseSample()
  58. {
  59.     using namespace CEGUI;
  60.  
  61.     // Get window manager which we wil use for a few jobs here.
  62.     WindowManager& winMgr = WindowManager::getSingleton();
  63.     // Load the scheme to initialse the VanillaSkin which we use in this sample
  64.     SchemeManager::getSingleton().loadScheme("../datafiles/schemes/VanillaSkin.scheme");
  65.     // set default mouse image
  66.     System::getSingleton().setDefaultMouseCursor("Vanilla-Images", "MouseArrow");
  67.  
  68.     // load an image to use as a background
  69.     ImagesetManager::getSingleton().createImagesetFromImageFile("BackgroundImage", "../datafiles/imagesets/GPN-2000-001437.tga");
  70.  
  71.     // here we will use a StaticImage as the root, then we can use it to place a background image
  72.     StaticImage* background = static_cast<StaticImage*>(winMgr.createWindow("Vanilla/StaticImage"));
  73.     // set area rectangle
  74.     background->setRect(Relative, Rect(0, 0, 1, 1));
  75.     // disable frame and standard background
  76.     background->setFrameEnabled(false);
  77.     background->setBackgroundEnabled(false);
  78.     // set the background image
  79.     background->setImage("BackgroundImage", "full_image");
  80.     // install this as the root GUI sheet
  81.     System::getSingleton().setGUISheet(background);
  82.  
  83.     FontManager::getSingleton().createFont("../datafiles/fonts/Iconified-12.font");
  84.  
  85.     // load some demo windows and attach to the background 'root'
  86.     background->addChildWindow(winMgr.loadWindowLayout("../datafiles/layouts/VanillaWindows.layout"));
  87.  
  88.     // create an instance of the console class.
  89.     d_console = new DemoConsole("Demo");
  90.  
  91.     // listen for key presses on the root window.
  92.     background->subscribeEvent(Window::EventKeyDown, Event::Subscriber(&FalagardDemo1Sample::handleRootKeyDown, this));
  93.  
  94.     // activate the background window
  95.     background->activate();
  96.  
  97.     // success!
  98.     return true;
  99. }
  100.  
  101.  
  102. /*************************************************************************
  103.     Cleans up resources allocated in the initialiseSample call.
  104. *************************************************************************/
  105. void FalagardDemo1Sample::cleanupSample()
  106. {
  107.     delete d_console;
  108. }
  109.  
  110.  
  111. bool FalagardDemo1Sample::handleRootKeyDown(const CEGUI::EventArgs& args)
  112. {
  113.     using namespace CEGUI;
  114.  
  115.     const KeyEventArgs& keyArgs = static_cast<const KeyEventArgs&>(args);
  116.  
  117.     switch (keyArgs.scancode)
  118.     {
  119.     case Key::F12:
  120.         d_console->toggleVisibility();
  121.         break;
  122.  
  123.     default:
  124.         return false;
  125.     }
  126.  
  127.     return true;
  128. }
  129.  
  130.  
  131. //////////////////////////////////////////////////////////////////////////
  132. /*************************************************************************
  133.  
  134.     DemoConsole class
  135.  
  136. *************************************************************************/
  137. //////////////////////////////////////////////////////////////////////////
  138. // these must match the IDs assigned in the layout
  139. const unsigned int DemoConsole::SubmitButtonID = 1;
  140. const unsigned int DemoConsole::EntryBoxID     = 2;
  141. const unsigned int DemoConsole::HistoryID      = 3;
  142.  
  143.  
  144. DemoConsole::DemoConsole(const CEGUI::String& id_name, CEGUI::Window* parent) :
  145.     d_root(CEGUI::WindowManager::getSingleton().loadWindowLayout("../datafiles/layouts/VanillaConsole.layout", id_name)),
  146.     d_historyPos(0)
  147. {
  148.     using namespace CEGUI;
  149.  
  150.     // we will destroy the console box windows ourselves
  151.     d_root->setDestroyedByParent(false);
  152.  
  153.     // Do events wire-up
  154.     d_root->subscribeEvent(Window::EventKeyDown, Event::Subscriber(&DemoConsole::handleKeyDown, this));
  155.  
  156.     d_root->getChild(SubmitButtonID)->
  157.         subscribeEvent(PushButton::EventClicked, Event::Subscriber(&DemoConsole::handleSubmit, this));
  158.  
  159.     d_root->getChild(EntryBoxID)->
  160.         subscribeEvent(Editbox::EventTextAccepted, Event::Subscriber(&DemoConsole::handleSubmit, this));
  161.  
  162.     // decide where to attach the console main window
  163.     parent = parent ? parent : CEGUI::System::getSingleton().getGUISheet();
  164.  
  165.     // attach this window if parent is valid
  166.     if (parent)
  167.         parent->addChildWindow(d_root);
  168. }
  169.  
  170. DemoConsole::~DemoConsole()
  171. {
  172.     // destroy the windows that we loaded earlier
  173.     CEGUI::WindowManager::getSingleton().destroyWindow(d_root);
  174. }
  175.  
  176. void DemoConsole::toggleVisibility()
  177. {
  178.     d_root->isVisible(true) ? d_root->hide() : d_root->show();
  179. }
  180.  
  181. bool DemoConsole::isVisible() const
  182. {
  183.     return d_root->isVisible();
  184. }
  185.  
  186. bool DemoConsole::handleSubmit(const CEGUI::EventArgs& args)
  187. {
  188.     using namespace CEGUI;
  189.  
  190.     // get the text entry editbox
  191.     Editbox* editbox = static_cast<Editbox*>(d_root->getChild(EntryBoxID));
  192.     // get text out of the editbox
  193.     String edit_text(editbox->getText());
  194.     // if the string is not empty
  195.     if (!edit_text.empty())
  196.     {
  197.         // add this entry to the command history buffer
  198.         d_history.push_back(edit_text);
  199.         // reset history position
  200.         d_historyPos = d_history.size();
  201.         // append newline to this entry
  202.         edit_text += '\n';
  203.         // get history window
  204.         MultiLineEditbox* history = static_cast<MultiLineEditbox*>(d_root->getChild(HistoryID));
  205.         // append new text to history output
  206.         history->setText(history->getText() + edit_text);
  207.         // scroll to bottom of history output
  208.         history->setCaratIndex(static_cast<size_t>(-1));
  209.         // erase text in text entry box.
  210.         editbox->setText("");
  211.     }
  212.  
  213.     // re-activate the text entry box
  214.     editbox->activate();
  215.  
  216.     return true;
  217. }
  218.  
  219. bool DemoConsole::handleKeyDown(const CEGUI::EventArgs& args)
  220. {
  221.     using namespace CEGUI;
  222.  
  223.     // get the text entry editbox
  224.     Editbox* editbox = static_cast<Editbox*>(d_root->getChild(EntryBoxID));
  225.  
  226.     switch (static_cast<const KeyEventArgs&>(args).scancode)
  227.     {
  228.     case Key::ArrowUp:
  229.         d_historyPos = ceguimax(d_historyPos - 1, -1);
  230.         if (d_historyPos >= 0)
  231.         {
  232.             editbox->setText(d_history[d_historyPos]);
  233.             editbox->setCaratIndex(static_cast<size_t>(-1));
  234.         }
  235.         else
  236.         {
  237.             editbox->setText("");
  238.         }
  239.  
  240.         editbox->activate();
  241.         break;
  242.  
  243.     case Key::ArrowDown:
  244.         d_historyPos = ceguimin(d_historyPos + 1, static_cast<int>(d_history.size()));
  245.         if (d_historyPos < static_cast<int>(d_history.size()))
  246.         {
  247.             editbox->setText(d_history[d_historyPos]);
  248.             editbox->setCaratIndex(static_cast<size_t>(-1));
  249.         }
  250.         else
  251.         {
  252.             editbox->setText("");
  253.         }
  254.  
  255.         editbox->activate();
  256.         break;
  257.  
  258.     default:
  259.         return false;
  260.     }
  261.  
  262.     return true;
  263. }
  264.